package cn.smartopen.aispace.interceptor;


import org.springframework.stereotype.Component;  
import org.springframework.web.servlet.HandlerInterceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;  
  
@Component  
public class LoginInterceptor implements HandlerInterceptor {  
  
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
        // 假设这里我们通过检查session或者token来判断用户是否登录  
        // 这里只是一个示例，具体实现取决于你的登录机制  
        Object loginStatus = request.getSession().getAttribute("loginStatus");  
        if (loginStatus==null || !(boolean)loginStatus) {  
            // 用户未登录  
            response.setContentType("application/json");  
            response.setCharacterEncoding("UTF-8");  
            response.getWriter().write("{\"code\":\"406\",\"message\":\"未登录\"}");  
            return false; // 阻止请求继续处理  
        }  
        // 用户已登录，继续请求处理  
        return true;  
    }  
}